package com.qianying.controller.web;

import com.qianying.entity.SysUser;
import com.qianying.service.SysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.qianying.comment.CommonResult;

import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping("index/")
@Tag(name = "用户登录注册", description = "登录和注册接口")
public class UserAuthController {

    @Autowired
    SysUserService userService;

    @Operation(summary = "用户登录", description = "使用用户名和密码登录，并从请求头获取MAC地址",
            parameters = {
                    @Parameter(name = "username", description = "用户名，系统登录账号", required= true),
                    @Parameter(name = "password", description = "密码，加密存储", required = true)}
    )
    @PostMapping("/login")
    public CommonResult<?> login(
            @RequestBody SysUser user,
            @Parameter(description = "客户端 MAC 地址", required = false)
            @RequestHeader(value = "X-MAC-Address", required = false) String macAddress,
            HttpServletRequest request) {
        return userService.login(user);

    }

    @PostMapping("/register")
    @Operation(summary = "用户注册",
            description = "用户注册",
            parameters = {
                    @Parameter(name = "username", description = "用户名，系统登录账号"),
                    @Parameter(name = "password", description = "密码，加密存储", required = true),
                    @Parameter(name = "roleId", description = "角色Id", required = true),
                    @Parameter(name = "enabled", description = "用户状态（0-无效用户，1-有效用户）", example = "1"),
                    @Parameter(name = "phone", description = "用户手机号码", example = "13800138000")}
    )
    public CommonResult<?> register(@RequestBody SysUser user) {
        try {
            return userService.register(user);
        } catch (RuntimeException e) {
            return CommonResult.failed(e.getMessage());
        }
    }
}
